package by.kniazhych.core.view.game.notifycations {
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	import by.kniazhych.core.server.common.UserNotifycation;
	import design.popups.GloryPopup;
	import design.popups.RokashPopup;
	import design.popups.ShahPopup;
	import design.popups.TronPopup;
	import design.popups.WinPopup;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.setTimeout;
	

	
	public class NotifycationFactory extends EventDispatcher{
		static public const HIDE_LAST_ONE:String = "hideLastOne";
		
		private var _popups:Object = new Object();
		private var _currentTimeout:uint;
		
		public function NotifycationFactory() {
			super(this);
			_popups[UserNotifycation.TRON] = new PopupView(TronPopup, 800);
			_popups[UserNotifycation.SHAH] = new PopupView(ShahPopup, 800);
			_popups[UserNotifycation.ROKASH] = new PopupView(RokashPopup, 800);
			_popups[UserNotifycation.DECLARE_KNIAZ] = new PopupView(GloryPopup, 800);
			_popups[UserNotifycation.VICTORY] = new PopupView(WinPopup);
		}
		
			
		public function getPopup(type:String):Sprite {
			if (_popups[type].timeout)
				_currentTimeout = setTimeout(_onPopupViewTimeout, _popups[type].timeout);
			return new _popups[type].popupClass();
		}
		
		private function _onPopupViewTimeout():void {
			dispatchEvent(new Event(HIDE_LAST_ONE));
		}
		
	}

}
//--------------------------------------------------------------------------
//
//  INNER DEFINITIONS
//
//--------------------------------------------------------------------------

internal final class PopupView {
	
	private var _popupClass:Class;
	private var _timeout:uint = 0;
	
	public function PopupView(cl:Class,t:uint = 0) {
		_popupClass = cl;
		_timeout = t;
	}
	
	public function get popupClass():Class {
		return _popupClass;
	}
	
	public function get timeout():uint {
		return _timeout;
	}
}
